//
//  ThreadVC.swift
//  SwfitDemo
//
//  Created by menchao on 2018/9/27.
//  Copyright © 2018年 Test. All rights reserved.
//

import UIKit

class ThreadVC: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.addSubview(self.clickButton)
        /*
        let date = Date()
        print(type(of: date))
 
        let global = DispatchQueue.global()
        for i in 0...10 {
            global.async {
               print("I am No.\(i), current thread name is:\(Thread.current)")
            }
        }
        
        let mainQueue = DispatchQueue.main
        for i in 0...10 {
            
            //使用主队列，开启异步任务
            //use the main queue, run in asynchronous
            mainQueue.async {
                print("I am No.\(i), current thread name is:\(Thread.current)")
                
            }
        }
        
        global.async {
            if let url = URL.init(string: "https://placebeard.it/355/140") {
                do {
                    let imageData = try Data(contentsOf: url)
                    let image = UIImage(data: imageData)
                    //因为宅胖家网络很好，为了模拟网络很耗时，就用了延时加载。
                    let now = DispatchTime.now()
                    let time = DispatchTimeInterval.seconds(2)
                    print("now:\(now),time:\(time)")
                    
                    print("current thread name is:\(Thread.current)")
                    
                    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() , execute: {
                        self.imageView.image = image
                          print("current thread name is:\(Thread.current)")
                    })
                } catch {
                    print(error)
                }
            }
        }
        
        */
        self.testThread2()
        print("执行结束")
    }
    
    
    func testThread1(){
        print("当前线程: \(Thread.current)")
        
        let queue = DispatchQueue(label: "创建串行队列",attributes: .concurrent)
        
        queue.async {
            print("串行队列中同步执行的第1个任务: \(Thread.current)")
            sleep(1)
        }
        
        queue.async {
            print("串行队列中同步执行的第2个任务: \(Thread.current)")
            sleep(1)
        }
        
        queue.async {
            print("串行队列中同步执行的第3个任务: \(Thread.current)")
        }
        
     }
    
    
    func testThread2(){
        
        let queue1:DispatchQueue = DispatchQueue.init(label: "textQueue1")
        let queue2:DispatchQueue = DispatchQueue.init(label: "textQueue2")
        let queue3:DispatchQueue = DispatchQueue.init(label: "textQueue3")
            // 队列queue加入队列组
        queue1.async (group: self.gropQueue){
            /*任务1*/
            print("queue1 start")
            
            self.gropQueue.enter()
            DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + DispatchTimeInterval.seconds(3), execute: {
                self.gropQueue.leave()
                 print("queue1  async")
            })
            print("queue1 end")
        }
        queue2.async (group: self.gropQueue){
            /*任务2*/
            print("queue2 start")
             self.gropQueue.enter()
            DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + DispatchTimeInterval.seconds(1), execute: {
                self.gropQueue.leave()
                print("queue2  async")
            })
            print("queue2 end")
        }
        queue3.async (group: self.gropQueue){
            /*任务3*/
            print("queue3 start")
            self.gropQueue.enter()
            DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + DispatchTimeInterval.seconds(2), execute: {
                self.gropQueue.leave()
                print("queue3  async")
            })
            print("queue3 end")
        }
        /*三个异步任务完成后执行界面处理操作*/
        self.gropQueue.notify(queue: DispatchQueue.main) {
            /*界面处理*/
            print("all queue notify")
        }
        
    }
    
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    @objc func clickMe(){
       self.testThread2()
    }
    
    
    lazy var gropQueue:DispatchGroup = {
        let grop:DispatchGroup = DispatchGroup.init()
        return grop
    }()
    
    
    lazy var imageView: UIImageView = {
        let imageView = UIImageView()
        self.view.addSubview(imageView)
        imageView.snp.makeConstraints({ (make) in
            make.left.equalTo(self.view).offset(30)
            make.top.equalTo(self.view).offset(100)
           
        })
        return imageView
    }()

    lazy var clickButton: UIButton = {
        let clickButton = UIButton(frame: CGRect.init(x: 0, y: 100, width: 300, height: 40))
        clickButton.setTitle("线程测试", for: UIControlState.normal)
        clickButton.backgroundColor = UIColor.blue
        clickButton.addTarget(self, action: #selector(clickMe), for: UIControlEvents.touchUpInside)
        return clickButton
    }()
}
